package ThaumPerditio.common.block;


import ThaumPerditio.common.ThaumPerditio;
import ThaumPerditio.common.lib.LibReference;
import ThaumPerditio.common.tile.TileDefusionPedestal;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.block.material.Material;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import thaumcraft.common.lib.utils.InventoryUtils;

import java.util.Random;

public class BlockDefusionDevice extends BlockContainer implements ITileEntityProvider{

    public BlockDefusionDevice() {
        super(Material.rock);
        this.setResistance(25.0F);
        this.setHardness(3.0F);
        this.setStepSound(soundTypeStone);
        this.setCreativeTab(ThaumPerditio.tabThaumPerditio);
    }

    @Override
    public TileEntity createNewTileEntity(World world, int metadata)
    {
        if(metadata==0) {return new TileDefusionPedestal();}
        else {return new TileDefusionPedestal();}
    }

    @Override
    public boolean isOpaqueCube() {
        return false;
    }

    @Override
    public boolean renderAsNormalBlock(){ return false; }

    @Override
    public int getRenderType() { return -1; }

    @Override
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int side, float par7, float par8, float par9) {
        if(world.isRemote) {
            return true;
        } else {
            int metadata = world.getBlockMetadata(x, y, z);
            TileEntity tileEntity = world.getTileEntity(x, y, z);
            if(metadata == 0 && tileEntity instanceof TileDefusionPedestal) {
                TileDefusionPedestal ped = (TileDefusionPedestal)tileEntity;
                if(ped.getStackInSlot(0) != null) {
                    InventoryUtils.dropItemsAtEntity(world, x, y, z, player);
                    world.playSoundEffect((double)x, (double)y, (double)z, "random.pop", 0.2F, ((world.rand.nextFloat() - world.rand.nextFloat()) * 0.7F + 1.0F) * 1.5F);
                    return true;
                }

                ItemStack istk;
                if(player.getCurrentEquippedItem() != null) {
                    istk = player.getCurrentEquippedItem().copy();
                    istk.stackSize = 1;
                    ped.setInventorySlotContents(0, istk);
                    --player.getCurrentEquippedItem().stackSize;
                    if(player.getCurrentEquippedItem().stackSize == 0) {
                        player.setCurrentItemOrArmor(0, (ItemStack)null);
                    }

                    player.inventory.markDirty();
                    world.playSoundEffect((double)x, (double)y, (double)z, "random.pop", 0.2F, ((world.rand.nextFloat() - world.rand.nextFloat()) * 0.7F + 1.0F) * 1.6F);
                    return true;
                }
                else {
                    return super.onBlockActivated(world, x, y, z, player, side, par7, par8, par9);
                }
            }
        }
        return true;
    }

    @Override
    public int damageDropped(int metadata) {
        return metadata == 3?7:(metadata == 4?6:metadata);
    }

    @Override
    public Item getItemDropped(int metadata, Random random, int fort) {
        return metadata != 3 && metadata != 4?super.getItemDropped(metadata, random, fort):Item.getItemFromBlock(LibReference.defusionDevice);
    }

    @Override
    public void breakBlock(World par1World, int par2, int par3, int par4, Block par5, int par6) {
        InventoryUtils.dropItems(par1World, par2, par3, par4);
        super.breakBlock(par1World, par2, par3, par4, par5, par6);
    }
}
